package com.victor.utils.wxlogin;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Formatter;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.victor.utils.HttpClientUtil;

public class WxController {

	private static Logger log = LoggerFactory.getLogger(WxController.class);

	public static SNSUserInfo weixinLogin(String code) throws Exception {
		String appid = "";
		String appsecret = "";
		Properties prop = new Properties();
		InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("appconfig.properties");
		try {
			prop.load(input);
			appid = prop.getProperty("appid");
			appsecret = prop.getProperty("appsecret");
			prop.clear();
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 获取网页授权access_token
		Oauth2Token oauth2Token = getOauth2AccessToken(appid, appsecret, code);
		// System.out.println("***********************************oauth2Token信息：" +
		// oauth2Token.toString());
		// 网页授权接口访问凭证
		String accessToken = oauth2Token.getAccessToken();
		// System.out.println("to值：" + accessToken);
		// 用户标识
		String openId = oauth2Token.getOpenId();
		// System.out.println("openid值：" + openId);
		// 获取用户信息
		SNSUserInfo snsUserInfo = getSNSUserInfo(accessToken, openId);
		return snsUserInfo;
	}

	/**
	 * 获取网页授权凭证
	 * 
	 * @param appId
	 *            公众账号的唯一标识
	 * @param appSecret
	 *            公众账号的密钥
	 * @param code
	 * @return WeixinAouth2Token
	 */
	public static Oauth2Token getOauth2AccessToken(String appId, String appSecret, String code) {
		Oauth2Token wat = null;
		// 拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
		requestUrl = requestUrl.replace("APPID", appId);
		requestUrl = requestUrl.replace("SECRET", appSecret);
		requestUrl = requestUrl.replace("CODE", code);
		// 获取网页授权凭证
		com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(NetUtil.get(requestUrl));
		if (null != jsonObject) {
			try {
				wat = new Oauth2Token();
				wat.setAccessToken(jsonObject.getString("access_token"));
				wat.setExpiresIn(jsonObject.getInteger("expires_in"));
				wat.setRefreshToken(jsonObject.getString("refresh_token"));
				wat.setOpenId(jsonObject.getString("openid"));
				wat.setScope(jsonObject.getString("scope"));
			} catch (Exception e) {
				wat = null;
				int errorCode = jsonObject.getInteger("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				log.error("获取网页授权凭证失败 errcode:{} errmsg:{}", errorCode, errorMsg);
			}
		}
		return wat;
	}

	/**
	 * 通过网页授权获取用户信息
	 * 
	 * @param accessToken
	 *            网页授权接口调用凭证
	 * @param openId
	 *            用户标识
	 * @return SNSUserInfo
	 */
	public static SNSUserInfo getSNSUserInfo(String accessToken, String openId) {
		SNSUserInfo snsUserInfo = null;
		// 拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
		requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
		// 通过网页授权获取用户信息
		com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(NetUtil.get(requestUrl));

		if (null != jsonObject) {
			try {
				snsUserInfo = new SNSUserInfo();
				// 用户的标识
				snsUserInfo.setOpenId(jsonObject.getString("openid"));
				// 昵称
				// emoji转码
				String nickname = jsonObject.getString("nickname");
				snsUserInfo.setNickname(java.net.URLEncoder.encode(nickname, "UTF-8"));
				// 性别（1是男性，2是女性，0是未知）
				snsUserInfo.setSex(jsonObject.getInteger("sex"));
				// 用户所在国家
				snsUserInfo.setCountry(jsonObject.getString("country"));
				// 用户所在省份
				snsUserInfo.setProvince(jsonObject.getString("province"));
				// 用户所在城市
				snsUserInfo.setCity(jsonObject.getString("city"));
				// 用户头像
				snsUserInfo.setHeadImgUrl(jsonObject.getString("headimgurl"));
				// System.out.println(snsUserInfo);
				// 用户特权信息
				List<String> list = JSON.parseArray(jsonObject.getString("privilege"), String.class);
				snsUserInfo.setPrivilegeList(list);
				// 与开放平台共用的唯一标识，只有在用户将公众号绑定到微信开放平台帐号后，才会出现该字段。
				snsUserInfo.setUnionid(jsonObject.getString("unionid"));
			} catch (Exception e) {
				snsUserInfo = null;
				int errorCode = jsonObject.getInteger("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				log.error("获取用户信息失败 errcode:{} errmsg:{}", errorCode, errorMsg);
			}
		}
		return snsUserInfo;
	}

	public static void main(String[] args) {
		getSNSUserInfo("18_8AxUV2PfyNcQr5Cr5du29ayFl8m2t2BzgEQcG7v_4ojW6qgWa01G7KQqalAlg1Uof_YHKoKWc0_YbDn-UcgpMQ",
				"oLEjk1JIfNjUgwnlUCiLFiVyotoA");
	}

	/**
	 * URL编码（utf-8）
	 * 
	 * @param source
	 * @return
	 */
	public static String urlEncodeUTF8(String source) {
		String result = source;
		try {
			result = java.net.URLEncoder.encode(source, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	private static String byteToHex(final byte[] hash) {
		Formatter formatter = new Formatter();
		for (byte b : hash) {
			formatter.format("%02x", b);
		}
		String result = formatter.toString();
		formatter.close();
		return result;
	}

	private static String create_nonce_str() {
		return UUID.randomUUID().toString();
	}

	private static String create_timestamp() {
		return Long.toString(System.currentTimeMillis() / 1000);
	}

	public static AppletInfo appletLogin(String code) {
		String appid = "";
		String appsecret = "";
		Properties prop = new Properties();
		InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("appconfig.properties");
		try {
			prop.load(input);
			appid = prop.getProperty("appletappid");
			appsecret = prop.getProperty("appletappsecret");
			prop.clear();
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String apiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + appsecret
				+ "&js_code=" + code + "&grant_type=authorization_code";
		String responseBody = HttpClientUtil.doGet(apiUrl);
		JSONObject jsonObject = JSON.parseObject(responseBody);

		AppletInfo result = null;
		if (null != jsonObject) {
			try {
				result = new AppletInfo();
				result.setOpenid(jsonObject.getString("openid"));
				result.setSession_key(jsonObject.getString("session_key"));
				result.setUnionid(jsonObject.getString("unionid"));
			} catch (Exception e) {
				result = null;
				int errorCode = jsonObject.getInteger("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				log.error("获取用户信息失败 errcode:{} errmsg:{}", errorCode, errorMsg);
			}
		}
		return result;
	}
}
